<template>
  <div class="hoseDefault">
    <!-- 搜索区域 -->
    <el-form ref="dataForm" class="hoseDefault_nav">
      <el-button
        v-if="button1"
        class="add"
        type="primary"
        size="medium"
        @click="creat"
      >添加一条</el-button>
    </el-form>

    <!-- 苑/期/区、幢、单元、室、楼段系数、电梯编号、物业类型（住宅、办公）、房屋状态（已售、出租）、房屋面积、收费面积、物业费方案、备注、操作（编辑、删除） -->
    <el-table :data="houseList" style="width: 100%" :border="true" :highlight-current-row="true">
      <el-table-column label="ID" width="60" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_id }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="区" width="150" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.community_id }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="期" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_court }}期</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="幢号" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_building }}幢</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="单元" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_single }}单元</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="室" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_rooms }}室</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="楼层系数" width="150" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_stairs }}层</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="电梯编号" width="140" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_elevator }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="物业类型(住宅/办公)" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_type }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="房屋状态(已售/出租)" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_state }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="房屋面积" width="120" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_measure }}m²</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="收费面积" width="120" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_charge }}m²</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="物业费方案" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_plan }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.area_remark }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-if="button2" size="small" type="success" @click="update(scope.row)">编辑</el-button>
          <el-button v-if="button3" size="small" type="danger" @click="deleteHouse(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 确认删除 -->
    <el-dialog :visible.sync="deleteDataFrom" width="30%">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <span>确定删除这一条吗？</span>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDataFrom = false">取消</el-button>
        <el-button type="primary" @click="deleteData">确定删除</el-button>
      </div>
    </el-dialog>
    <!-- 分页器 -->
    <el-col :span="24" class="toolbar" style="text-align: center;">
      <el-pagination
        background
        :current-page="currentPage"
        :page-count="pageNum"
        :page-size="pagesize"
        style="margin-top:30px;margin-bottom:30px"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-col>
  </div>
</template>
<script>
import request from '@/utils/request'
import editHouseDefault from '../editHouseDefault'
import { getToken, getAccount, getButtonpermsStr } from '@/utils/auth'
export default {
  components: {
    editHouseDefault
  },
  data() {
    return {
      houseList: [],
      total: 0,
      currentPage: 1, // 初始页
      pagesize: 10, // 当前页面内的列表行数
      pageNum: 1, // 总数页数
      deleteDataFrom: false,
      rules: {},
      button1: false,
      button2: false,
      button3: false,
      buttonpermsStr: getButtonpermsStr(),
      temp: {
        area_id: '',
        area_block: '',
        area_court: '',
        area_unit: '',
        area_rooms: '',
        area_building: '',
        area_elevator: '',
        area_type: 0,
        area_state: 0,
        area_measure: '',
        area_charge: '',
        area_programme: '',
        area_remarks: ''
      }
    }
  },
  mounted() {
    if (this.buttonpermsStr !== undefined) {
      if (this.buttonpermsStr.indexOf(140) > -1) {
        this.button1 = true
      }
      if (this.buttonpermsStr.indexOf(141) > -1) {
        this.button2 = true
      }
      if (this.buttonpermsStr.indexOf(142) > -1) {
        this.button3 = true
      }
    }
    this.getHouseList()
  },
  methods: {
    // 添加
    creat() {
      this.temp = {
        area_id: '',
        area_block: '',
        area_court: '',
        area_unit: '',
        area_rooms: '',
        area_building: '',
        area_elevator: '',
        area_type: 0,
        area_state: 0,
        area_measure: '',
        area_charge: '',
        area_programme: '',
        area_remarks: ''
      }
      this.$router.push({ name: '编辑房屋信息', params: { data: this.temp }})
    },
    // 修改
    update(row) {
      this.temp = Object.assign(row)
      this.$router.push({ name: '编辑房屋信息', params: { data: this.temp }})
    },
    // 当前页
    handleSizeChange: function(pagesize) {
      this.pagesize = pagesize
      this.getHouseList()
    },
    // 换页：更新列表数据
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
      this.getHouseList()
    },
    // 删除
    deleteHouse(row) {
      this.deleteDataFrom = true
      this.temp = {
        area_id: row.area_id
      }
    },
    // 确定删除
    deleteData() {
      // return request({
      //   url: "/v3/admin/area/delete",
      //   method: "post",
      //   headers: {
      //     account_type: getAccount()
      //   },
      //   data: this.temp
      // }).then(res => {
      //   if (res.tip === "迁出成功.") {
      //     this.deleteDataFrom = false;
      //     this.$router.go(0); //刷新页面
      //   }
      // });
    },
    // 获取房屋列表
    getHouseList() {
      return request({
        url: '/v3/areas/list',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: {
          current_page: this.currentPage,
          page_size: this.pagesize,
          area_id: 0
        }
      }).then(res => {
        this.currentPage = res.current_page
        this.pageNum = res.pages
        this.total = res.total_record
        res.areasLists.map((value, index) => {
          if (value.area_remarks === null) {
            value.area_remarks = '---'
          }
          if (value.community_id == 1) {
            value.community_id = '经纬创意园'
          } else if (value.community_id == 2) {
            value.community_id = '凯华街道'
          } else if (value.community_id == 4) {
            value.community_id = '杨家村小区'
          } else if (value.community_id == 5) {
            value.community_id = '大都会风情苑'
          }
          if (value.area_type == '0') {
            value.area_type = '住宅'
          } else {
            value.area_type = '办公'
          }
          if (value.area_state == '0') {
            value.area_state = '已售'
          } else if (value.area_state == '1') {
            value.area_state = '出租'
          } else {
            value.area_state = '空置'
          }
        })
        this.houseList = res.areasLists
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.hoseDefault {
  margin-top: 55px;
  overflow: hidden;
  .hoseDefault_nav {
    margin: 10px;
  }
}
</style>
